home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1996 September / MACPOWER-1996-09.ISO.7z / MACPOWER-1996-09.ISO / 第2特集:プラグイン大集合 / PIC2 Save / ExpMain.c next >
Text File  |  1995-01-14  |  7KB  |  335 lines

  1. /*
  2.     File: ExpMain.c (Export Module main procedure)
  3.  
  4.     Copyright © 1995 K&M Software Corporation (番犬 JBE01713@nifty)
  5.     Portions  © 1992 Adobe Systems Incorporated
  6.                      Apple Computer, Inc.
  7.               © 1994 Metrowerks, Inc.
  8.     PIC2 Format ©         やなぎさわ
  9.  
  10.     1995/01/14    new!
  11. */
  12.  
  13. // This file defines some constant procedure which slows your compile.
  14. // Since they are not changed and refered objects only, we divide it.
  15.  
  16. #ifdef __MWERKS__
  17. #include <A4Stuff.h>
  18. #endif
  19.  
  20. #include "pic2.h"
  21. #include "PIC2PlugIn.h"
  22.  
  23. /*****************************************************************************/
  24.  
  25. /* All calls to the plug-in module come through this routine. It must be
  26.    placed first in the resource. To achieve this, most development systems
  27.    require that this be the first routine in the source. */
  28.  
  29. QDGlobals qd;
  30.  
  31. static asm void    asm_copyqd(void)
  32. {
  33.             MOVE.L    (A5),A0         // Get address of real quickdraw globals
  34.             SUB.L    #126,A0         // Move to start
  35.             LEA     qd.randSeed,A1     // Get address of local copy
  36.             MOVE.W    #64,D0            // Globals are 65 words long
  37. @1:         MOVE.W    (A0)+,(A1)+     // Copy a word
  38.             DBF     D0,@1            // Move to next word
  39.     rts
  40. }
  41.  
  42. pascal void ENTRYPOINT(short selector,ExportRecordPtr stuff,long *data,short *result)
  43. {
  44.  
  45.     /* Set up the globals. */
  46.  
  47.     GHdl globals;
  48.     
  49. #ifdef __MWERKS__
  50.     long oldA4 = SetCurrentA4();
  51.     asm_copyqd();
  52. #endif
  53.     
  54.     if (!*data)
  55.     {
  56.  
  57.         *data = (long) NewHandleClear(sizeof(Globals));
  58.         
  59.         if (!*data)
  60.         {
  61.             *result = memFullErr;
  62.             return;
  63.         }
  64.         HNoPurge( (Handle) *data);
  65.         HLock( (Handle) *data);
  66.         InitGlobals((GHdl) *data);
  67.         
  68.     }
  69.  
  70.     globals = (GHdl) *data;
  71.     HLock((Handle)globals);
  72.     
  73.     /* Perform the requested operation */
  74.  
  75.     gStuff    = stuff;
  76.  
  77.     switch (selector)
  78.     {
  79.  
  80.     case exportSelectorAbout:
  81.         DoAbout(globals);
  82.         break;
  83.  
  84.     case exportSelectorPrepare:
  85.         DoPrepare(globals);
  86.         break;
  87.  
  88.     case exportSelectorStart:
  89.         DoStart(globals);
  90.         break;
  91.  
  92.     case exportSelectorContinue:
  93.         DoContinue(globals);
  94.         break;
  95.  
  96.     case exportSelectorFinish:
  97.         DoFinish(globals);
  98.         break;
  99.  
  100.     default:
  101.         gResult = exportBadParameters;
  102.  
  103.     }
  104.     *result = gResult;
  105.     HUnlock((Handle)globals);
  106.  
  107. #if    defined(__MWERKS__)
  108.     SetA4(oldA4);                        // Does not support PC-relative string index
  109. #else
  110.     RestoreA4 ();
  111. #endif
  112.  
  113. }
  114.  
  115. /*****************************************************************************/
  116.  
  117. /* UserItem to outline the OK button in a dialog box. */
  118.  
  119. pascal void static_OutlineOK(DialogPtr dp, short item)
  120. {
  121.     
  122.     PenState originalPenState;
  123.  
  124.     Rect r;
  125.     Handle h;
  126.     short itemType;
  127.     
  128.     GetPenState (&originalPenState);
  129.  
  130.     item = ok;
  131.  
  132.     GetDItem (dp, item, &itemType, &h, &r);
  133.  
  134.     PenNormal ();
  135.     PenSize (3, 3);
  136.     
  137.     InsetRect (&r, -4, -4);
  138.     FrameRoundRect (&r, 16, 16);
  139.  
  140.     SetPenState (&originalPenState);
  141.     
  142. }
  143.  
  144. /*****************************************************************************/
  145.  
  146. void    SetOutlineOKHook(DialogPtr dp, short hookItem)
  147. {
  148.     
  149.     short itemType;
  150.     Rect r;
  151.     Handle h;
  152.  
  153.     GetDItem (dp, hookItem, &itemType, &h                  , &r);
  154.     SetDItem (dp, hookItem,  itemType, (Handle) &static_OutlineOK, &r);
  155.     
  156. }
  157.  
  158. /*****************************************************************************/
  159.  
  160. /* Calls the host's TestAbort function */
  161.  
  162. pascal Boolean DoTestAbort (ProcPtr codeAddress) = {0x205F, 0x4E90};
  163.  
  164. /* Calls the host's TestAbort function */
  165.  
  166. Boolean TestAbort (GHdl globals)
  167. {
  168.     return DoTestAbort(gStuff->abortProc);
  169. }
  170.  
  171. /*****************************************************************************/
  172.  
  173. pascal void DoUpdateProgress (long done, long total, ProcPtr codeAddress) = {0x205F, 0x4E90};
  174.  
  175. /* Calls the host's UpdateProgress procedure */
  176.  
  177. void UpdateProgress(GHdl globals,long done, long total)
  178. {
  179.     DoUpdateProgress (done, total, gStuff->progressProc);
  180. }
  181.  
  182. /*****************************************************************************/
  183.  
  184. /* Centers a dialog template 1/3 of the way down on the main screen. */
  185.  
  186. /* The following routine locates the QuickDraw globals. */
  187. // See TN M.PT.StandAloneCode, Technical Notes #110
  188.  
  189. typedef struct QDVars
  190. {
  191.     char privates[76];
  192.     long randSeed;
  193.     BitMap screenBits;
  194.     Cursor arrow;
  195.     Pattern dkGray;
  196.     Pattern ltGray;
  197.     Pattern gray;
  198.     Pattern black;
  199.     Pattern white;
  200.     GrafPtr thePort;
  201. } QDVars;
  202.  
  203. static QDVars *GetQDVars(void)
  204. {
  205.     
  206.     return (QDVars *) ((* (char **) SetCurrentA5 ()) -
  207.                                     (sizeof (QDVars) - sizeof (GrafPtr)));
  208.     
  209. }
  210.  
  211. /*****************************************************************************/
  212.  
  213. /* Set the cursor to the arrow cursor. */
  214.  
  215. void SetArrowCursor(void)
  216. {
  217.     QDVars *qd = GetQDVars();
  218.     
  219.     SetCursor (&(qd->arrow));
  220. }
  221.  
  222. /*****************************************************************************/
  223.  
  224. #define menuHeight 20
  225.  
  226. void CenterDialog (DialogTHndl dt)
  227. {
  228.  
  229.     Rect r;
  230.     short width;
  231.     short height;
  232.     
  233.     QDVars *qd = GetQDVars ();
  234.     
  235.     width  = qd->screenBits.bounds.right;
  236.     height = qd->screenBits.bounds.bottom;
  237.  
  238.     r = (**dt).boundsRect;
  239.     OffsetRect (&r, -r.left, -r.top);
  240.     OffsetRect (&r, (width - r.right) / 2,
  241.                     (height - r.bottom - menuHeight) / 3 + menuHeight);
  242.     (**dt).boundsRect = r;
  243.  
  244. }
  245.  
  246. #undef menuHeight
  247.  
  248. /*****************************************************************************/
  249.  
  250. void    ReplyToSpec(FSSpec *spec, SFReply *reply)
  251. {
  252.     short        aVolNum;
  253.     long        aDirID, aProcID;
  254.  
  255.     GetWDInfo(reply->vRefNum, &aVolNum, &aDirID, &aProcID);
  256.     BlockMove(reply->fName, spec->name, reply->fName[0]+1);
  257.     spec->vRefNum = aVolNum;
  258.     spec->parID = aDirID;
  259. }
  260.  
  261. /*****************************************************************************/
  262.  
  263. /* Displays the about dialog box for the plug-in module. */
  264.  
  265.  
  266. void DoAbout(GHdl globals)
  267. {
  268.     short item;
  269.     DialogPtr dp;
  270.     DialogTHndl dt;
  271.  
  272.     gResult = noErr;
  273.     
  274.     dt = (DialogTHndl) GetResource ('DLOG', kAboutDialogID);
  275.     HNoPurge ((Handle) dt);
  276.     CenterDialog (dt);
  277.  
  278.     dp = GetNewDialog (kAboutDialogID, nil, (WindowPtr) -1);
  279.     ParamText((StringPtr)cPIC2SaveVersion,(StringPtr)NULL,(StringPtr)NULL,(StringPtr)NULL);
  280.     ModalDialog (nil, &item);
  281.  
  282.     DisposDialog (dp);
  283.     HPurge ((Handle) dt);
  284. }
  285.  
  286. /*****************************************************************************/
  287.  
  288. /* Displays the error dialog box for the plug-in module. */
  289.  
  290. #define OKhookItem 3
  291. void DoError(GHdl globals,StringPtr p1,StringPtr p2)
  292. {
  293.     short item;
  294.     DialogPtr dp;
  295.     DialogTHndl dt;
  296.     Str15        st3;
  297.     
  298.     dt = (DialogTHndl) GetResource ('DLOG', kErrorDialogID);
  299.     HNoPurge ((Handle) dt);
  300.     CenterDialog (dt);
  301.  
  302.     dp = GetNewDialog (kErrorDialogID, nil, (WindowPtr) -1);
  303.     if(p2==NULL)    p2=(StringPtr)st3;
  304.     st3[0]=0;
  305.     ParamText(p1,p2,NULL,NULL);
  306.     SetOutlineOKHook(dp,OKhookItem);
  307.     ModalDialog (nil, &item);
  308.  
  309.     DisposDialog (dp);
  310.     HPurge ((Handle) dt);
  311. }
  312.  
  313. #undef OKhookItem
  314.  
  315. // Color conversion
  316.  
  317. unsigned short    nbitsto8(unsigned short xbits, short n)
  318. {
  319.     unsigned short    p,q;
  320.     short    i;
  321.     if(n==0 || n>=8)    return    xbits;
  322. #if 1
  323.     p=0;
  324.     q=xbits << (8-n);
  325.     for(i=8/n;i>=0;i--)
  326.     {
  327.         p >>= n;
  328.         p |= q;
  329.     }    
  330.     return    p;
  331. #else
  332.     return    xbits;
  333. #endif
  334. }
  335.